package 前K个高频元素;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 主要就是会使用优先队列
 * @date 2022/10/24 18:28
 */
public class Demo347 {
    public int[] topKFrequent(int[] nums, int k) {
        HashMap<Integer,Integer> map=new HashMap();
        for (int num : nums) {
            map.put(num,map.getOrDefault(num,0)+1);
        }
        PriorityQueue<Map.Entry<Integer, Integer>> priorityQueue=new PriorityQueue<>((s0, s1)->s1.getValue()-s0.getValue());
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        for (Map.Entry<Integer, Integer> entry : entries) {
            priorityQueue.add(entry);
        }
        int[] ins=new int[k];
        for (int i = 0; i <k ; i++) {
            ins[i]=priorityQueue.poll().getKey();
        }
        return ins;
    }
}
